package com.example.bausip;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddTaskActivity extends Activity {

	private DatePicker datePicker;
	private Calendar calendar;
	private Button dateView, upload;
	private int year, month, day;
	int RESULT_LOAD_IMAGE = 1;
	EditText task_name ,task_body;
	Spinner mytype;
	int course_id ;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_task);
		calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DAY_OF_MONTH);
		dateView = (Button) findViewById(R.id.setDate);
		
		task_name = (EditText) findViewById(R.id.uniIdInput);
		task_body = (EditText) findViewById(R.id.nameinput);
		mytype = (Spinner) findViewById(R.id.spinner1);
		
		Bundle extras = getIntent().getExtras();
		if(extras!=null)
		{
			course_id = extras.getInt("course_id");
		}

		showDate(year, month + 1, day);

		upload = (Button) findViewById(R.id.upload);
		upload.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, RESULT_LOAD_IMAGE);
			}
		});
	}
	public void addTask(View v)
	{
		String taskName = task_name.getText().toString();
		String taskBody = task_body.getText().toString();
		String tasktype = mytype.getSelectedItem().toString();
		Task myTask = new Task();
		myTask.setName(taskName);
		myTask.setDescription(taskBody);
		myTask.setType(tasktype);
		int type_id = 0;
		if(myTask.getType().equals("Assignment"))
		{
			type_id = 1;
		}
		else
		{
			type_id = 2;
		}
		String serverURL = new myURL("add_task.php?task_title="+myTask.getName()+
				"&task_description="+myTask.getDescription()+"&course_id="+course_id+"&task_type_id="+type_id+"task_due_date"+dateView.getText().toString(),true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "done", true);
		p.get();
		
	}
	public void done(String s,String error)
	{
		Toast.makeText(getApplicationContext(), "Task submitted successfully", Toast.LENGTH_SHORT)
	      .show();
		Intent intent = new Intent(this, CourseActivity.class);
		intent.putExtra("course_id", course_id);
		startActivity(intent);
	}

	@SuppressWarnings("deprecation")
	public void setDate(View view) {
		showDialog(999);
		Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
				.show();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		if (id == 999) {
			return new DatePickerDialog(this, myDateListener, year, month, day);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
			// TODO Auto-generated method stub
			// arg1 = year
			// arg2 = month
			// arg3 = day
			showDate(arg1, arg2 + 1, arg3);
		}
	};

	private void showDate(int year, int month, int day) {
		dateView.setText(new StringBuilder().append(day).append("/")
				.append(month).append("/").append(year));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.student_info, menu);
		return true;
	}

}
